<template>
  <div>
    <MBX level1="商品管理" level2="商品分类"></MBX>
    <el-card class="mt16">
      <el-button type="primary" @click="addcate">添加分类</el-button>
      <el-table :data="tableData" row-key="cat_id" style="width: 100%">
        <el-table-column label="索引" type="index">
          <template slot-scope="scope">
            <div>
              {{ scope.row.index }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
        <el-table-column label="是否有效">
          <template slot-scope="scope">
            <div>
              <i v-if="scope.row.cat_deleted" class="el-icon-success"></i>
              <i else class="el-icon-error"></i>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="排序">
          <template slot-scope="scope">
            <div>
              <el-tag :type="typtObj[scope.row.cat_level]">{{
                scope.row.cat_level | rank
              }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="260">
          <!-- <template> -->
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="editRow(scope.row)"
              type="primary"
              size="small"
            >
              编辑
            </el-button>

            <el-button
              @click.native.prevent="del(scope.row)"
              type="danger"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加 -->
      <el-dialog
        @close="close"
        title="添加分类"
        :visible.sync="dialogVisible"
        width="50%"
      >
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="分类名称">
            <el-input v-model="form.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <!-- value选择后的 -->
            <!-- options数据源 -->
            <!-- props配置项 -->
            <el-cascader
              ref="jilian"
              v-model="value"
              :options="options"
              :props="defultin"
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addok">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pages.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pages.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 编辑 -->
      <el-dialog title="编辑分类" :visible.sync="editdialogVisible" width="50%">
        <el-form ref="editform" :model="editform" label-width="80px">
          <el-form-item label="分类名称">
            <el-input v-model="editform.cat_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editdialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editok">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import { shopcateApi, addcateApi, editcateApi, delcateApi } from '@/http/api'
export default {
  name: 'catehoriesA',
  data() {
    return {
      typtObj: {
        0: 'primary',
        1: 'success',
        2: 'warning'
      },
      pages: {
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      tableData: [],
      // 添加
      dialogVisible: false,
      form: {
        cat_pid: '',
        cat_name: '',
        cat_level: ''
      },
      // 配置项
      defultin: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        // 可以选中
        checkStrictly: true
      },
      options: [],
      // 添加的级联数据
      value: [],
      // 编辑
      editform: {
        cat_name: ''
      },
      editdialogVisible: false
    }
  },
  created() {
    this.getshopcate()
  },
  methods: {
    // 删除
    async del(row) {
      console.log(row)
      const res = await delcateApi(row.cat_id)
      console.log(res)
      this.getshopcate()
    },
    // 编辑
    editRow(row) {
      this.editform.cat_name = row.cat_name
      this.editform.id = row.cat_id
      this.editdialogVisible = true
    },
    async editok() {
      this.editdialogVisible = false
      console.log(this.editform)
      const res = await editcateApi(this.editform)
      console.log(res)
      this.getshopcate()
    },
    // addok添加
    async addok() {
      this.dialogVisible = false
      if (this.value.length !== 0) {
        console.log('不为0')
        this.form.cat_pid = this.value.at(-1)
        this.form.cat_level = this.value.length
        console.log(this.form)
        const res = await addcateApi(this.form)
        console.log(res)
        this.getshopcate()
      } else {
        this.form.cat_pid = 0
        this.form.cat_level = 0
        const res = await addcateApi(this.form)
        console.log(res)
        this.getshopcate()
      }
    },
    close() {
      // 清空
      this.form = {}
      this.value = ''
    },
    // 分页
    handleSizeChange(val) {
      this.pages.pagesize = val
      this.getshopcate()
    },
    handleCurrentChange(val) {
      this.pages.pagenum = val
      this.getshopcate()
    },
    // 渲染
    async getshopcate() {
      const res = await shopcateApi(this.pages)
      console.log(res)
      const { total, result } = res
      result.forEach((item, index) => {
        item.index = index + 1
      })
      this.total = total
      this.tableData = result
    },
    // 添加分类
    // 级联选择器数据
    async addcate() {
      this.dialogVisible = true
      const res = await shopcateApi({ type: 2 })
      console.log(res)
      this.options = res
    },
    // 点击级联 消失
    handleChange() {
      console.log(this.value)
      // console.log('级联', this.$refs.jilian)
      this.$refs.jilian.dropDownVisible = false
    }
  }
}
</script>
<style>
.el-scrollbar__wrap {
  height: 200px;
}
/* 小框 */
.el-radio {
  position: absolute;
  width: 70%;
  height: 30px;
}
/* 隐藏 */
.el-radio__inner {
  display: none;
}
</style>

<style lang="scss" scoped>
.el-card {
  text-align: left;
}
.el-icon-success {
  color: #0f0;
}
.el-icon-error {
  color: #f00;
}
.el-cascader {
  width: 100%;
}
</style>
